package com.tangula.web.kashine.business;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tangula.core.Logging;
import com.tangula.web.form.DefaultPagingBody;
import com.tangula.web.kashine.entity.ClassStudentRefEntity;
import com.tangula.web.kashine.entity.DeleteStudentForm;
import com.tangula.web.kashine.entity.LoadTeacherStudentForm;
import com.tangula.web.kashine.entity.UpDateStudentForm;
import com.tangula.web.kashine.repository.ClassStudentRefEntityRepository;
import com.tangula.web.kashine.repository.UpDateSudentFormRepository;
@Service
public class ClassStudentRefBusinessImpl implements ClassStudentRefBusiness, Logging{
	@Autowired
	ClassStudentRefEntityRepository classStudentRepository;
	@Autowired
	UpDateSudentFormRepository updateRepo;
	@Override
	public DefaultPagingBody<UpDateStudentForm> showStudentInClass(LoadTeacherStudentForm clas) {
		if(clas!=null) {
			List<UpDateStudentForm> udsf = updateRepo.showStudentInClass(clas.getCondition(),clas.getSize());
			DefaultPagingBody<UpDateStudentForm> dpb = new DefaultPagingBody<>(clas.getIndex(),clas.getSize(),udsf.size(),udsf);
			return dpb;
		}else {
			return null;
		}
	}
	@Override
	public ClassStudentRefEntity changeClassStudent(ClassStudentRefEntity classStudent) {
		if(classStudent!=null) {
			ClassStudentRefEntity student = classStudentRepository.findByClassIdAndStudentId(classStudent.getClassId(),classStudent.getStudentId());
			student.setStudentName(classStudent.getStudentName());
			student.setStudentDesc(classStudent.getStudentDesc());
			return classStudentRepository.saveAndFlush(student);
		}else {
			return null;
		}
	}
	@Override
	public Boolean deleteStudentFromClass(DeleteStudentForm classStudent) {
		if(classStudent!=null) {
			//logDebug("deleteStudentFromClass:"+JSONObject.toJSONString(classStudent));
			ClassStudentRefEntity asr = classStudentRepository.findByClassIdAndStudentId(classStudent.getClassId(), classStudent.getStudentId());
			//logDebug("deleteStudentFromClass entity:"+JSONObject.toJSONString(asr));
			classStudentRepository.deleteById(asr.getId());
		}
		return true;
	}
}
